//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "schedulerctrl.h"
#include "macinputdlg.h"
#include <wx/regex.h>

enum cIDS { cIDEnableScheduling=13001, cIDSchedulingCtrl};

BEGIN_EVENT_TABLE(CMACInputDlg, wxDialog)
	EVT_UPDATE_UI(wxID_OK, CMACInputDlg::OnOKUpdateUI)
	EVT_UPDATE_UI(cIDSchedulingCtrl, CMACInputDlg::OnSchedUpdateUI)
END_EVENT_TABLE()


CMACInputDlg::CMACInputDlg(bool bWhite, MACENTRY& entry, bool bEdit):
	wxDialog( (wxWindow *)NULL, wxID_ANY, wxString(_("Input MAC Address"))),
	m_bWhite(bWhite), m_entry(entry)
{
	wxBoxSizer* szMain = new wxBoxSizer(wxVERTICAL);

	//MAC
	wxBoxSizer* szMAC = new wxBoxSizer(wxHORIZONTAL);
	wxStaticText* pST0 = new wxStaticText(this, wxID_ANY, _("MAC:"));
	szMAC->Add( pST0, 0, wxRIGHT, 10 );
	m_ptcMAC = new wxTextCtrl( this, wxID_ANY, wxString::FromAscii( entry.strMAC.c_str()),
		wxDefaultPosition, wxSize(150, 20), bEdit?wxTE_READONLY:0 );

	if ( bEdit )
	{
		m_ptcMAC->Enable(false);
	}


	szMAC->Add( m_ptcMAC );
	szMain->Add(szMAC, 0, wxLEFT|wxRIGHT|wxUP, 10);
	szMain->AddSpacer(5);

	wxStaticText* pstL0 = new wxStaticText(this, wxID_ANY, _("Example: 00:1B:47:01:AE:CE"));
	szMain->Add(pstL0, 0, wxLEFT|wxRIGHT|wxDOWN, 10);

	if ( bWhite )
	{
		m_pcbEnableSched = new wxCheckBox(this, cIDEnableScheduling, _("Enable scheduling"));
		szMain->Add(m_pcbEnableSched, 0, wxALL, 10);
		szMain->AddSpacer(5);

		m_pcbEnableSched->SetValue( entry.bEnableSched );

		//Scheduling control
		m_pctrScheduler = new CSchedulerCtrl(this, cIDSchedulingCtrl, m_entry.arrSched);
		szMain->Add(m_pctrScheduler, 0, wxLEFT|wxRIGHT|wxDOWN, 10);
	}

	wxSizer* szButtons = CreateStdDialogButtonSizer(wxOK|wxCANCEL);
	szMain->Add(szButtons, 0, wxALL, 10);

	SetSizer(szMain);
	szMain->Fit(this);
}

CMACInputDlg::~CMACInputDlg(void)
{
}

bool CMACInputDlg::Show()
{
	bool bRes =	wxID_OK==ShowModal();

	if ( bRes )
	{
		m_entry.strMAC = m_ptcMAC->GetValue().ToAscii();

		if ( m_bWhite )
		{
			m_entry.bEnableSched = m_pcbEnableSched->GetValue();
		}
	}

	return bRes;
}

////////////////////////////////////////////////////////////////////////////
//Msg handler
void CMACInputDlg::OnOKUpdateUI(wxUpdateUIEvent& ev)
{
	wxString strMAC = m_ptcMAC->GetValue();
	//wxRegEx regexp(wxT("^([0-9a-fA-F]{2}([:]|$)){6}"), wxRE_DEFAULT);
	wxRegEx regexp(wxT("^([0-9A-F]{2}([:]|$)){6}"), wxRE_DEFAULT);
	ev.Enable( regexp.Matches(strMAC) );
}

void CMACInputDlg::OnSchedUpdateUI(wxUpdateUIEvent& event)
{
	if ( m_pcbEnableSched )
	{
		event.Enable( m_pcbEnableSched->GetValue() );
	}
}

